import { _decorator,screen,Tween,view,Label,Widget, assetManager,Camera, Node,Color,math, Sprite,SystemEvent, LabelComponent, SkinnedMeshRenderer,Prefab,v3, Vec3,SkeletalAnimation, ProgressBar, tween, UITransform } from 'cc';

import BaseView from '../../Script/game/BaseView';
import {Bullet} from '../../Script/config/BaseData';
import Tool from '../../Script/tool/Tool'
// import { luckyBox } from './luckyBox';
import GD from '../../Script/config/GD';
import GDef from '../../Script/config/GDef';
import SdkMgr from '../../Script/tyq3.x/SdkMgr';
import { tyqSDK } from '../../Script/tyq3.x/tyq-sdk';
import TyqEventMgr from '../../Script/tyq3.x/tyq-event-mgr';
const { bits } = math;
const { ccclass, property } = _decorator;

@ccclass('preMain')
export class preMain extends BaseView {
    @property({
        type: Node
    })
    nodeAll: Node = null;
    @property({
        type: Node
    })
    nodeLevel: Node = null;
    @property({
        type: Label
    })
    labelLevel: Label = null;
    @property({
        type: Node
    })
    btnStart: Node = null;
    @property({
        type: Node
    })
    btnCastHuman: Node = null;
    @property({
        type: Node
    })
    btnCastDino: Node = null;
    @property({
        type: Node
    })
    nodeCastHumanFree: Node = null;
    @property({
        type: Node
    })
    nodeCastHumanCoin: Node = null;
    @property({
        type: Label
    })
    labelCastHumanCoin: Label = null;
    @property({
        type: Node
    })
    nodeCastHunterNum: Node = null;
    @property({
        type: Label
    })
    labelCastHunterNum: Label = null;
    @property({
        type: Node
    })
    nodeCastDinoFree: Node = null;
    @property({
        type: Node
    })
    nodeCastDinoCoin: Node = null;
    @property({
        type: Label
    })
    labelCastDinoCoin: Label = null;
    @property({
        type: Node
    })
    nodeCastDinoNum: Node = null;
    @property({
        type: Label
    })
    labelCastDinoNum: Label = null;
    @property({
        type: Node
    })
    nodeCoinBarParent: Node = null;
    @property({
        type: Node
    })
    nodeProgressParent: Node = null;
    @property({
        type: Node
    })
    btnRound: Node = null;

    onLoad() {
        this.bindButton(this.btnStart, this.onClickStart);
        this.bindButton(this.btnCastHuman, this.onClickHuman);
        this.bindButton(this.btnCastDino, this.onClickDino);
        this.bindButton(this.btnRound, this.onClickRound);
        this.on(GDef.wdEvent.updateCoin,this.onUpdateCoin);
        this.on(GDef.wdEvent.levelStart,this.onLevelStart);
        this.on(GDef.wdEvent.addTeach,this.addTeach);
        this.on(GDef.wdEvent.updateCastDino,this.updateCastDino);
        this.on(GDef.wdEvent.updateCastHunter,this.updateCastHuman);
        this.on(GDef.wdEvent.addCastDinoNum,this.addCastDinoNum);
        this.on(GDef.wdEvent.addCastHunterNum,this.addCastHunterNum);
        
        //添加金币
        var self = this;
        if(GDef.nodeCoinBar)GDef.nodeCoinBar.parent = self.nodeCoinBarParent;
        else
        {
            Tool.createNode(GDef.pack1,GDef.preCoinBar).then((node) =>{
                GDef.nodeCoinBar = node;
                node.parent = self.nodeCoinBarParent;
            }).catch(err => {
                console.error(err);
            });
        }
        //添加进度
        if(GDef.nodeProgress)GDef.nodeProgress.parent = self.nodeProgressParent;
        else
        {
            Tool.createNode(GDef.pack1,GDef.preProgress).then((node) =>{
                GDef.nodeProgress = node;
                node.parent = self.nodeProgressParent;
            }).catch(err => {
                console.error(err);
            });
        }
        this.labelLevel.string = "" + GD.getLevel();
        //根据分辨率调大小
        let size = null;
        if(view.getVisibleSize())size = view.getVisibleSize();
        else size = screen.windowSize;

        // console.log(size);
        // const Framesize = view.getVisibleSize();
        // console.log(Framesize);
        // const Framesize1 = view.getVisibleSizeInPixel();
        // console.log(Framesize1);
        let ratio = Math.min(1,size.height / 1660);
        this.btnCastHuman.scale = v3(ratio * 0.9,ratio * 0.9,ratio * 0.9);
        this.btnCastDino.scale = v3(ratio * 0.9,ratio * 0.9,ratio * 0.9);
        this.btnStart.scale = v3(ratio,ratio,ratio);
        this.btnStart.getComponent(Widget).bottom = this.btnCastHuman.getComponent(UITransform).height * ratio * 0.9 + 20 * ratio * ratio;
        this.nodeLevel.scale = v3(ratio,ratio,ratio);
        this.nodeCoinBarParent.scale = v3(Math.min(1,1.2 * ratio),Math.min(1,1.2 * ratio),Math.min(1,1.2 * ratio));
        this.nodeProgressParent.scale = v3(ratio,ratio,ratio);
        this.nodeProgressParent.getComponent(Widget).top = this.nodeLevel.getComponent(UITransform).height * ratio + 40 * ratio * ratio * ratio * ratio;

    }
    onEnable(){
        this.onLevelStart();
    }
    addTeach(){
        //显示拖动合成
        if(GD.intTeach == 0)
        {
            GD.addTeach();
        }
        //显示点击战斗
        else if(GD.intTeach == 1)
        {
            GD.addTeach();
            this.btnCastDino.active = true;
            this.btnCastHuman.active = true;
            this.btnStart.active = true;
            Tool.showHand(this.btnStart,"dianji");
            Tool.grayNode(this.btnCastDino,true);
            Tool.grayNode(this.btnCastHuman,true);
            Tool.grayNode(this.btnStart,false);
        }
        else if(GD.intTeach == 2)
        {
            GD.addTeach();
            this.btnCastDino.active = true;
            this.btnCastHuman.active = true;
            this.btnStart.active = true;
            Tool.grayNode(this.btnCastDino,false);
            Tool.grayNode(this.btnCastHuman,false);
            Tool.removeHand();
        }
    }
    onLevelStart() {
        this.labelLevel.string = "" + GD.getLevel();
        this.updateCastHuman();
        this.updateCastDino();
        //根据教学隐藏按钮
        //新手第1步 点击召唤恐龙
        if(GD.intTeach == 0)
        {
            this.btnCastDino.active = true;
            this.btnCastHuman.active = true;
            this.btnStart.active = true;
            Tool.grayNode(this.btnCastDino,false);
            Tool.grayNode(this.btnCastHuman,true);
            Tool.grayNode(this.btnStart,true);
            Tool.showHand(this.btnCastDino,"dianji");
        }
        //教学  合成恐龙
        else if(GD.intTeach == 1)
        {
            this.btnCastDino.active = false;
            this.btnCastHuman.active = false;
            this.btnStart.active = false;
        }
        //显示点击战斗
        else if(GD.intTeach == 2)
        {
            this.btnCastDino.active = true;
            this.btnCastHuman.active = true;
            this.btnStart.active = true;
            Tool.grayNode(this.btnCastDino,true);
            Tool.grayNode(this.btnCastHuman,true);
            Tool.grayNode(this.btnStart,false);
            Tool.showHand(this.btnStart,"dianji");
        }
        this.nodeAll.active = true;
    }
    onClickStart() {
        //新手第一步 点击开始
        //新手第4步 点击开始
        if(GD.intTeach >= 2)
        {
            if(GD.intTeach == 2)
            {
                this.emit(GDef.wdEvent.addTeach);
            }
            this.nodeAll.active = false;
            this.emit(GDef.wdEvent.startFight);
        }
    }
    onClickHuman() {
        //新手第3步之后
        if(GD.intTeach > 2)
        {
            //召唤
            //有免费次数
            if(GD.getSavedHunter().length > 0)
            {
                this.emit(GDef.wdEvent.castHuman);
                //更新按钮状态
                this.updateCastHuman();
                this.updateCastDino();
            }
            else
            {
                let cost = GD.getCastHumanCost();
                if(GD.getCoin() >= cost)
                {
                    let switchCastSpecial = tyqSDK.getConfigProbability('switchCastSpecialHunter');
                    if(GD.getCastHuman() == 1)switchCastSpecial = true;
                    GD.addSavedHunter(1 + (switchCastSpecial?1:0));
                    this.emit(GDef.wdEvent.addCoin,-cost);
                    this.emit(GDef.wdEvent.castHuman);
                    GD.addCastHuman();
                    //更新按钮状态
                    this.addCastHunterNum();
                    this.updateCastHuman();
                    this.updateCastDino();
                }
                //看广告
                else
                {
                    let self = this;
                    TyqEventMgr.ins.sendEvent('观看激励视频-获取弓箭手');
                    SdkMgr.ins.showRewardAd("观看激励视频-获取弓箭手", () => {
                        TyqEventMgr.ins.sendEvent('观看完激励视频-获取弓箭手');
                        let addTimes = GD.getAdCastHunter();
                        this.emit(GDef.wdEvent.showFrameArm,1,1,addTimes);
                        for(let i = 0; i < addTimes; i++)GD.addSavedHunter(1 + (tyqSDK.getConfigProbability('switchCastSpecialHunter')?1:0));
                        if(addTimes == 1)self.emit(GDef.wdEvent.castHuman);
                        GD.addCastHunterAdCount();
                        //更新按钮状态
                        self.addCastHunterNum();
                        self.updateCastHuman();
                        self.updateCastDino();
                    },()=>{},()=>{});
                }
            }
        }
    }
    onClickDino() {
        //新手第3步
        if(GD.intTeach == 0 || GD.intTeach > 2)
        {
            //召唤
            //有免费次数
            if(GD.getSavedDino().length > 0)
            {
                this.emit(GDef.wdEvent.castDino);
                //更新按钮状态
                this.updateCastHuman();
                this.updateCastDino();
            }
            else
            {
                //金币
                let cost = GD.getCastDinoCost();
                if(GD.getCoin() >= cost)
                {
                    if(GD.intTeach == 0)
                    {
                        this.emit(GDef.wdEvent.addTeach);
                        Tool.grayNode(this.btnCastDino,true);
                        Tool.grayNode(this.btnCastHuman,true);
                        Tool.grayNode(this.btnStart,true);
                        this.btnCastDino.active = false;
                        this.btnCastHuman.active = false;
                        this.btnStart.active = false;
                    }
                    let switchCastSpecial = tyqSDK.getConfigProbability('switchCastSpecialHunter');
                    if(GD.getCastDino() == 2)switchCastSpecial = true;
                    GD.addSavedDino(1 + (switchCastSpecial?1:0));
                    this.emit(GDef.wdEvent.addCoin,-cost);
                    this.emit(GDef.wdEvent.castDino);
                    GD.addCastDino();
                    //更新按钮状态
                    this.addCastDinoNum();
                    this.updateCastHuman();
                    this.updateCastDino();
                }
                //看广告
                else
                {
                    let self = this;
                    TyqEventMgr.ins.sendEvent('观看激励视频-获取恐龙');
                    SdkMgr.ins.showRewardAd("观看激励视频-获取恐龙", () => {
                        TyqEventMgr.ins.sendEvent('观看完激励视频-获取恐龙');
                        let addTimes = GD.getAdCastDino();
                        this.emit(GDef.wdEvent.showFrameArm,2,1,addTimes);
                        for(let i = 0; i < addTimes; i++)GD.addSavedDino(1 + (tyqSDK.getConfigProbability('switchCastSpecialHunter')?1:0));
                        if(addTimes == 1)self.emit(GDef.wdEvent.castDino);
                        GD.addCastDinoAdCount();
                        //更新按钮状态
                        self.addCastDinoNum();
                        self.updateCastHuman();
                        self.updateCastDino();
                    });
                }
            }
        }
    }
    onUpdateCoin() {
        this.updateCastHuman();
        this.updateCastDino();
    }
    updateCastHuman(){
        //有免费次数
        if(GD.getSavedHunter().length > 0)
        {
            this.nodeCastHumanFree.active = false;
            this.nodeCastHumanCoin.active = false;
            this.nodeCastHunterNum.active = true;
            this.labelCastHunterNum.string = "" + GD.getSavedHunter().length;
        }
        else
        {
            let cost = GD.getCastHumanCost();
            if(GD.getCoin() < cost)
            {
                this.nodeCastHumanFree.active = true;
                this.nodeCastHumanCoin.active = false;
                this.nodeCastHunterNum.active = false;
            }
            else{
                this.nodeCastHumanFree.active = false;
                this.nodeCastHumanCoin.active = true;
                this.nodeCastHunterNum.active = false;
                this.labelCastHumanCoin.string = "" + Tool.formatNum(cost);
            }
        }
        //如果有空位则显示按钮,否则隐藏
        if(GD.getPlayerFreeCellIndex() >= 0)
        {
            this.btnCastHuman.active = true;
        }
        else
        {
            this.btnCastHuman.active = false;
        }
    }
    updateCastDino(){
        //有免费次数
        if(GD.getSavedDino().length > 0)
        {
            this.nodeCastDinoFree.active = false;
            this.nodeCastDinoCoin.active = false;
            this.nodeCastDinoNum.active = true;
            this.labelCastDinoNum.string = "" + GD.getSavedDino().length;
        }
        else
        {
            let cost = GD.getCastDinoCost();
            if(GD.getCoin() < cost)
            {
                this.nodeCastDinoFree.active = true;
                this.nodeCastDinoCoin.active = false;
                this.nodeCastDinoNum.active = false;
            }
            else{
                this.nodeCastDinoFree.active = false;
                this.nodeCastDinoCoin.active = true;
                this.nodeCastDinoNum.active = false;
                this.labelCastDinoCoin.string = "" + Tool.formatNum(cost);
            }
        }
        //如果有空位则显示按钮,否则隐藏
        if(GD.getPlayerFreeCellIndex() >= 0)
        {
            this.btnCastDino.active = true;
        }
        else
        {
            this.btnCastDino.active = false;
        }
    }
    addCastHunterNum(){
        let num = GD.getSavedHunter().length;
        let preNum = parseInt(this.labelCastHunterNum.string);
        let addNum = num - preNum;
        this.nodeCastDinoFree.active = false;
        this.nodeCastDinoCoin.active = false;
        this.nodeCastHunterNum.active = true;
        var self = this;
        const tweenTargetVec32 = new Vec3();
        this.tweenProgressHunter = new Tween(tweenTargetVec32)
            .by(0.5, new Vec3(1, 1, 1), {
                'onUpdate': (target: object | undefined, ratio: number | undefined) => {
                    let n = Math.floor(preNum + ratio * addNum);
                    self.labelCastHunterNum.string = Tool.formatNum(n) + "";
                }
            }).call(()=>{
                if(parseInt(self.labelCastHunterNum.string) <= 0)self.updateCastHuman();
            });
        this.tweenProgressHunter.start();

    }
    addCastDinoNum(){
        let num = GD.getSavedDino().length;
        let preNum = parseInt(this.labelCastDinoNum.string);
        let addNum = num - preNum;
        this.nodeCastDinoFree.active = false;
        this.nodeCastDinoCoin.active = false;
        this.nodeCastDinoNum.active = true;
        var self = this;
        const tweenTargetVec32 = new Vec3();
        this.tweenProgress = new Tween(tweenTargetVec32)
            .by(0.5, new Vec3(1, 1, 1), {
                'onUpdate': (target: object | undefined, ratio: number | undefined) => {
                    let n = Math.floor(preNum + ratio * addNum);
                    self.labelCastDinoNum.string = Tool.formatNum(n) + "";
                }
            }).call(()=>{
                if(parseInt(self.labelCastDinoNum.string) <= 0)self.updateCastDino();
            });
        this.tweenProgress.start();

    }
    onClickRound(){
        //清理战场
        let len = GDef.nodeArms.children.length;
        for(let i = len - 1; i >= 0; i--)
        {
            if(i < GDef.nodeArms.children.length && GDef.nodeArms.children[i])GDef.poolArm.put(GDef.nodeArms.children[i]);
        }
        GD.addLevel();
        this.emit(GDef.wdEvent.levelStart);
    }
}   
